package cn.gailvlun.gll.presentation.emotion.notification

import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import cn.gailvlun.gll.R
import cn.gailvlun.gll.chatlib.NotificationType
import cn.gailvlun.gll.chatlib.bean.SystemNotification
import cn.gailvlun.gll.databinding.ActivityEmotionNotificationBinding
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.emotion.core.BaseAdapter
import cn.gailvlun.gll.presentation.emotion.core.ViewModelFactory
import cn.gailvlun.gll.presentation.hotel.widget.DividerDecoration
import cn.gailvlun.gll.util.DialogUtil
import cn.gailvlun.gll.util.LogUtil
import io.realm.RealmResults



class EmotionNotificationActivity : BaseActivity() {
    private lateinit var mBinding: ActivityEmotionNotificationBinding
    private lateinit var mAdapter: EmotionNotificationAdapter
    private var allEmotions: MutableList<SystemNotification> = ArrayList()
    private lateinit var fromActivity: FromActivityEnum

    private lateinit var mViewModel: EmotionNotificationViewModel

    companion object {

        fun openActivity(activity: BaseActivity, fromActivity: FromActivityEnum) {
            val intent = Intent(activity, EmotionNotificationActivity::class.java)
            intent.putExtra("from_activity", fromActivity)
            activity.startActivity(intent)
        }

    }

    enum class FromActivityEnum {
        FROM_SQUARE,
        FROM_PERSONAL
    }

    override fun initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_emotion_notification)
        mAdapter = EmotionNotificationAdapter(this)
        mViewModel = ViewModelProviders.of(this, ViewModelFactory.getInstance(this, mAdapter as BaseAdapter<Any>)).get(EmotionNotificationViewModel::class.java)

        mAdapter.setPresenter(mViewModel)
        mAdapter.setDecorator { holder, _, _ ->
            val itemBinding = holder.binding
        }
        mBinding.rvEmotionList.adapter = mAdapter
        mBinding.rvEmotionList.layoutManager = LinearLayoutManager(mContext)
        mBinding.rvEmotionList.addItemDecoration(DividerDecoration(mContext,R.color.emotion_licorice))


    }

    override fun createHandler() {
        mBinding.nbv.setOnBackListener { onBackPressed() }

    }

    override fun loadData(savedInstanceState: Bundle?) {
        fromActivity = intent.getSerializableExtra("from_activity") as FromActivityEnum
        val pd = DialogUtil.showIndeterminate(mContext)

        var readcount = 0
        var total = 0
        mViewModel.searchSystemNotification()
                ?.subscribe({ systemNotifications: RealmResults<SystemNotification>? ->
                    pd.dismiss()
                    mAdapter.clear()
                    systemNotifications
                            ?.asSequence()
                            ?.filter { it.type == NotificationType.emotionHeart  || it.type ==NotificationType.emotionComment }
                            ?.forEach {
                                when (fromActivity) {
                                    FromActivityEnum.FROM_PERSONAL -> {
                                        


                                        mAdapter.add(it, EmotionNotificationAdapter.Type.HEART.ordinal)

                                        total++



                                    }
                                    FromActivityEnum.FROM_SQUARE -> {
                                        if (it.isUnread) {

                                            mAdapter.add(it, EmotionNotificationAdapter.Type.HEART.ordinal)

                                        } else {
                                            readcount++
                                        }
                                        total++
                                        allEmotions.add(it)
                                    }
                                }
                            }
                    if (readcount > 0) {
                        mAdapter.add(null, EmotionNotificationAdapter.Type.MORE.ordinal)
                    }
                    if (total == 0) {
                        mAdapter.add(null, EmotionNotificationAdapter.Type.EMPTY.ordinal)
                    }
                }, { t: Throwable? ->
                    LogUtil.e(t?.message)
                }, {

                })
    }


    fun loadMore() {
        mAdapter.clear()
        mAdapter.addAll(allEmotions, EmotionNotificationAdapter.Type.HEART.ordinal)
        mAdapter.notifyDataSetChanged()
    }

}